package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    /*rabbit消息队列*/
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        String companyId = getLoginUserCompanyId();
        PageInfo<User> pageInfo = userService.findByPage(pageNum,pageSize,companyId);
        request.setAttribute("pageInfo",pageInfo);
        return "system/user/user-list";
    }

    /*
      url:/system/user/list.do
      作用： 进入添加页面,存储域数据：企业的所有用户
      参数： 无
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        String companyId = getLoginUserCompanyId();
        List<Dept> deptList = deptService.findAll(companyId);
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }

    /*
      url:/system/user/edit.do
      作用： 有传用户id时为修改信息，没有传用户id时为添加用户信息
      参数： User user
     */
    @RequestMapping("/edit")
    public String edit(User user){
        //补全用户信息
        String companyId = getLoginUserCompanyId();
        String companyName = getLoginUserCompanyName();
        user.setCompanyId(companyId);
        user.setCompanyName(companyName);

        String id = user.getId();

        if (StringUtils.isEmpty(id)){
            //新增用户信息
            userService.save(user);
            //convertAndSend(“交换机名字”,"路由key名字",发送的消息);实现添加用户，发送邮件欢迎
            rabbitTemplate.convertAndSend("email-exchange","user.add",user);
        }else {
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }


    /*
      url:/system/user/toUpdate.do?id=3d00290a-1af0-4c28-853e-29fbf96a2722
      作用： 进入编辑页面,根据用户id查找用户信息，数据回显
      参数： 无
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        User user = userService.findById(id);
        request.setAttribute("user",user);

        String companyId = getLoginUserCompanyId();
        List<Dept> deptList = deptService.findAll(companyId);
        request.setAttribute("deptList",deptList);
        return "system/user/user-update";
    }



    /*
      url:/system/user/delete.do?id=100
      作用： 删除用户
      参数： 用户id
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String id){
        HashMap<String, Object> resultMap = new HashMap<>();
        Boolean flag = userService.delete(id);
        if (flag){
            resultMap.put("message","删除成功");
        }else {
            resultMap.put("message","已分配角色，删除失败");
        }
        resultMap.put("flag",flag);
        return resultMap;
    }

    /*
          url:/system/user/roleList.do?id=002108e2-9a10-4510-9683-8d8fd1d374ef
          作用： 进入用户角色管理页面
          参数： 用户id
         */
    @RequestMapping("/roleList")
    public String roleList(String id){
        //1、根据用户id查找该用户，回显信息
        User user = userService.findById(id);
        request.setAttribute("user",user);

        //2、查找该企业所有角色
        List<Role> roleList = roleService.findAll(getLoginUserCompanyId());
        request.setAttribute("roleList",roleList);

        //3、根据用户id查找该用户已分配的角色
        List<Role> userRoleList = roleService.findUserRoleByUserId(id);
        request.setAttribute("userRoleList",userRoleList);
        return "system/user/user-role";
    }

    /*
      url:
      /system/user/changeRole.do?
      userid=002108e2-9a10-4510-9683-8d8fd1d374ef
      &roleIds=4028a1c34ec2e5c8014ec2ebf8430001
      &roleIds=4028a1c34ec2e5c8014ec2ec38cc0002
      &roleIds=4028a1cd4ee2d9d6014ee2df4c6a0000
      作用： 更新用户角色
      参数： userid 用户id  roleIds 角色id
     */
    @RequestMapping("/changeRole")
    public String changeRole(String userid,String[] roleIds){
        userService.changeRole(userid,roleIds);
        return "redirect:/system/user/list.do";
    }
}
